package com.txx.app.main.modules.web.function;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Environment;
import android.text.TextUtils;

import com.qihoo360.replugin.RePlugin;
import com.txx.app.main.App;
import com.txx.app.main.Constant;
import com.txx.app.main.commom.bean.HasVisitingCardBean;
import com.txx.app.main.commom.bean.VisitingInfoBean;
import com.txx.app.main.commom.constant.AppVisitingCardConstant;
import com.txx.app.main.commom.net.HttpUtil;
import com.txx.app.main.modules.web.WebCommonFragment;
import com.txx.app.main.modules.web.js.H5BasePlugin;
import com.txx.app.main.modules.web.js.H5Event;

import org.json.JSONException;
import org.json.JSONObject;

import okhttp3.MediaType;
import okhttp3.RequestBody;
import rx.Subscriber;

/**
 * created by houyl
 * on  6:51 PM
 */
public class JumpToMyCard extends H5BasePlugin {

    @Override
    public boolean handleEvent(H5Event h5Event, WebCommonFragment fragment) {
        if (h5Event == null) {
            return false;
        }

        String channel = App.perInfo.getChannel ();
        if ("3".equals (channel) || "1".equals (channel)||"a".equals(channel)) {
            //银保经代
            checkHasYBJDVisitingCard (fragment.getContext(), channel);
        } else {
            //个险电销
            checkHadGXDXVisitingCard (fragment.getContext());
        }

        return true;
    }

    @Override
    public void onRelease() {

    }

    @Override
    public String getCallBackName() {
        return null;
    }

    private void checkHasYBJDVisitingCard(final Context context, final String channel) {
        JSONObject jsonObject = new JSONObject ();
        String staffNumber = App.staffBuidler;
        try {
            jsonObject.put ("staffNumber", staffNumber);
        } catch (JSONException e) {
            e.printStackTrace ();
        }
        RequestBody requestBody = RequestBody.create (MediaType.parse ("application/json; charset=utf-8"), jsonObject.toString ());
        HttpUtil.init (HttpUtil.sspHttpUtils ().hasVisitingCard (requestBody), new Subscriber<HasVisitingCardBean>() {
            @Override
            public void onCompleted() {
            }

            @Override
            public void onError(Throwable e) {
                if (((Activity) context).isFinishing ()) {
                    return;
                }
            }

            @Override
            public void onNext(HasVisitingCardBean hasVisitingCardBean) {
                if (((Activity) context).isFinishing ()) {
                    return;
                }
                switch (hasVisitingCardBean.getRspCode ()) {
                    case Constant.GET_DATA_SUCCESS:
                        HasVisitingCardBean.InfoBean info = hasVisitingCardBean.getInfo ();
                        if (info != null) {
                            String flag = info.getFlag ();
                            if ("Y".equals (flag)) {
                                if ("1".equals (channel)||"a".equals(channel)) {
                                    //跳转到经代名片H5
                                    Intent jumpIntent = RePlugin.createIntent ("app.selfcenter", "com.txx.app.selfcenter.my_visiting_card.activity.MyJDVisitingCardActivity");
                                    if (null != jumpIntent) {
                                        String path1 = Environment.getExternalStorageDirectory ().getPath ().concat ("/app.selfcenter-Etest-debug.apk");
                                        jumpIntent.putExtra (AppVisitingCardConstant.HAS_CARD_TAG, true);
                                        RePlugin.startActivity (context, jumpIntent);
                                    }
                                } else if ("3".equals (channel)) {
                                    //跳转到银保名片H5
                                    Intent jumpIntent = RePlugin.createIntent ("app.selfcenter", "com.txx.app.selfcenter.my_visiting_card.activity.MyYBVisitingCardActivity");
                                    if (null != jumpIntent) {
                                        String path1 = Environment.getExternalStorageDirectory ().getPath ().concat ("/app.selfcenter-Etest-debug.apk");
                                        jumpIntent.putExtra (AppVisitingCardConstant.HAS_CARD_TAG, true);
                                        RePlugin.startActivity (context, jumpIntent);
                                    }
                                }
                            } else if ("N".equals (flag)) {
                                if ("1".equals (channel)||"a".equals(channel)) {
                                    //跳转到经代名片编辑页
                                    Intent jumpIntent = RePlugin.createIntent ("app.selfcenter", "com.txx.app.selfcenter.my_visiting_card.activity.EditJDVisitingCardActivity");
                                    if (null != jumpIntent) {
                                        String path1 = Environment.getExternalStorageDirectory ().getPath ().concat ("/app.selfcenter-Etest-debug.apk");
                                        jumpIntent.putExtra (AppVisitingCardConstant.HAS_CARD_TAG, false);
                                        RePlugin.startActivity (context, jumpIntent);
                                    }
                                } else if ("3".equals (channel)) {
                                    //跳转到银保名片编辑页
                                    Intent jumpIntent = RePlugin.createIntent ("app.selfcenter", "com.txx.app.selfcenter.my_visiting_card.activity.EditVisitingCardYBActivity");
                                    if (null != jumpIntent) {
                                        String path1 = Environment.getExternalStorageDirectory ().getPath ().concat ("/app.selfcenter-Etest-debug.apk");
                                        jumpIntent.putExtra (AppVisitingCardConstant.HAS_CARD_TAG, false);
                                        RePlugin.startActivity (context, jumpIntent);
                                    }
                                }
                            } else {
                            }
                        }
                        break;
                }
            }
        });
    }

    /**
     * 检查是否有名片
     */
    private void checkHadGXDXVisitingCard(final Context context) {
//        String staffNumber = App.staffBuidler;
        HttpUtil.init (HttpUtil.sspHttpUtils ().getVisitingCardInfo (), new Subscriber<VisitingInfoBean> () {

            @Override
            public void onCompleted() {
            }

            @Override
            public void onError(Throwable e) {
            }

            @Override
            public void onNext(VisitingInfoBean visitingInfoBean) {
                switch (visitingInfoBean.getRspCode ()) {
                    case Constant.GET_DATA_SUCCESS:
                        //个险不会为空
                        if ("2".equals (App.perInfo.getChannel ())) {
                            //判断微信二维码是否为空,跳转到编辑界面
                            String wx2DImgUrl = visitingInfoBean.getInfo ().getWx2DImgUrl ();
                            String wxNum = visitingInfoBean.getInfo ().getWxNum ();
                            String workAge = visitingInfoBean.getInfo ().getWorkAge ();
                            String tags = visitingInfoBean.getInfo ().getTags ();
                            String headImgUrl = visitingInfoBean.getInfo ().getHeadImgUrl ();
                            String myPersonWord = visitingInfoBean.getInfo ().getMotto ();
                            String enterCompanyTime = visitingInfoBean.getInfo ().getDteapp ();
                            String starlevel = visitingInfoBean.getInfo ().getStarlevel ();
                            String contmonth = visitingInfoBean.getInfo ().getContmonth ();
                            String months = visitingInfoBean.getInfo ().getMonths ();
                            String year = visitingInfoBean.getInfo ().getYear ();
                            String serverCustomNum = visitingInfoBean.getInfo ().getClntCount ();
                            String customValueNum = visitingInfoBean.getInfo ().getSumins ();
                            String reportNum = visitingInfoBean.getInfo ().getAccumpolicy ();
                            String select2 = visitingInfoBean.getInfo ().getPerformanceFlag ();
                            String workLevelFlag = visitingInfoBean.getInfo ().getLevelFlag ();
                            String enterConpanyFlag = visitingInfoBean.getInfo ().getRsTimeFlag ();
                            String tkStarFlag = visitingInfoBean.getInfo ().getTkStarFlag ();

                            if (TextUtils.isEmpty (visitingInfoBean.getInfo ().getWx2DImgUrl ())) {
                                Intent intent = RePlugin.createIntent ("app.selfcenter", "com.txx.app.selfcenter.my_visiting_card.activity.EditVisitingGXActivity");
                                if (intent == null) {
                                    return;
                                }
                                intent.putExtra (AppVisitingCardConstant.INFO_IS_COMPLETE, true);
                                intent.putExtra (AppVisitingCardConstant.WX_IMAGE_URL, wx2DImgUrl);
                                intent.putExtra (AppVisitingCardConstant.HEAD_IMAGE_URL, headImgUrl);
                                intent.putExtra (AppVisitingCardConstant.MY_PERSON_WORD, myPersonWord);
                                intent.putExtra (AppVisitingCardConstant.ENTER_COMPANY, enterCompanyTime);
                                intent.putExtra (AppVisitingCardConstant.STAR_LEVEL, starlevel);
                                intent.putExtra (AppVisitingCardConstant.COUNT_MONTH, contmonth);
                                intent.putExtra (AppVisitingCardConstant.MONTH, months);
                                intent.putExtra (AppVisitingCardConstant.WORK_YEAR, year);
                                intent.putExtra (AppVisitingCardConstant.REPORT_NUM, reportNum);
                                intent.putExtra (AppVisitingCardConstant.CUSTOM_VALUE, customValueNum);
                                intent.putExtra (AppVisitingCardConstant.SERVER_NUM, serverCustomNum);
                                intent.putExtra (AppVisitingCardConstant.SELECT_2, select2);
                                intent.putExtra (AppVisitingCardConstant.IS_SHOW_WORK_LEVEL, workLevelFlag);
                                intent.putExtra (AppVisitingCardConstant.IS_SHOW_ENTER_TIME, enterConpanyFlag);
                                intent.putExtra (AppVisitingCardConstant.IS_SHOW_STAR, tkStarFlag);
                                intent.putExtra (AppVisitingCardConstant.INFO_IS_COMPLETE, false);
                                if (null != intent) {
                                    String path1 = Environment.getExternalStorageDirectory ().getPath ().concat ("/app.selfcenter-Etest-debug.apk");
                                    RePlugin.startActivity (context, intent);
                                }
                                return;
                            } else {
                                Intent intent = RePlugin.createIntent ("app.selfcenter", "com.txx.app.selfcenter.my_visiting_card.activity.MyVisitingCardActivity");
                                if (null != intent) {
                                    String path1 = Environment.getExternalStorageDirectory ().getPath ().concat ("/app.selfcenter-Etest-debug.apk");
                                    RePlugin.startActivity (context, intent);
                                }
                            }
                        } else if ("4".equals (App.perInfo.getChannel ())) {
                            if (visitingInfoBean.getInfo () == null) {
                                Intent intent = RePlugin.createIntent ("app.selfcenter", "com.txx.app.selfcenter.my_visiting_card.activity.EditVisitingCardActivity");
                                if (intent != null) {
                                    intent.putExtra (AppVisitingCardConstant.INFO_IS_COMPLETE, false);
                                    String path1 = Environment.getExternalStorageDirectory ().getPath ().concat ("/app.selfcenter-Etest-debug.apk");
                                    RePlugin.startActivity (context, intent);
                                }
                                return;
                            } else {
                                Intent intent = RePlugin.createIntent ("app.selfcenter", "com.txx.app.selfcenter.my_visiting_card.activity.MyVisitingCardActivity");
                                if (intent != null) {
                                    String path1 = Environment.getExternalStorageDirectory ().getPath ().concat ("/app.selfcenter-Etest-debug.apk");
                                    RePlugin.startActivity (context, intent);
                                }
                            }
                        }
                        break;
                }
            }
        });
    }


}
